
var contactPeopleArray=null;
var globle_contactpeopletown=[];
var globle_contactpeoplevillage=[];
var contactpeopletable =function(){
	contactPeopleArray = [];
	$("#globleJqgridDataContactPeople").css('display','');
	$("#globleJqgridDataDisplay").css('display','none');
	$("#globleJqgridDataSchool1").css('display','none');
	$("#globleJqgridDataSchool2").css('display','none');
	$("#globleJqgridDataSchool3").css('display','none');
	$("#globleJqgridDataSchool4").css('display','none');
	$("#globleJqgridDataSchool5").css('display','none');
	$("#globleJqgridDataPowerStation").css('display','none');
	$("#globleJqgridDataRailway").css('display','none');
	$("#globleJqgridDataGasStation").css('display','none');
	$("#globleJqgridDataRestArea").css('display','none');
	$("#globleJqgridDataFireStation").css('display','none');
	$("#globleJqgridDataHospital1").css('display','none');
	$("#globleJqgridDataHospital2").css('display','none');
	$("#globleJqgridDataHospital3").css('display','none');
//	//$("#globleJqgridData").remClass('display');
//	jQuery("#list2").jqGrid("clearGridData");
//    $("#list2").empty();
//    $("#pager2").empty();
//    jQuery("#list2").trigger("reloadGrid");
	//数据加载
	//globle_contactpeopletown.splice(0,globle_contactpeopletown.length);
	//globle_contactpeoplevillage.splice(0,globle_contactpeoplevillage.length);
	
	globle_contactpeopletown=Global.areaQuery.features.people.town;
	globle_contactpeoplevillage=Global.areaQuery.features.people.village;
	globle_contactpeopletown.push.apply(globle_contactpeopletown,globle_contactpeoplevillage);
	//alert(globle_contactpeopletown);
	for(var i=0 ; i < globle_contactpeopletown.length; i++){
		globle_contactpeopletown[i].data.ID=i+1;
		//alert(globle_contactpeopletown[i].data);
		contactPeopleArray[i]=globle_contactpeopletown[i].data;
	}
//	for(var i=0 ; i < contactPeopleArray.length; i++){
//		//contactPeopleArray[i].ID=i+1;
//		alert(contactPeopleArray[i].ID);
//	}
//	
    //页面加载完成之后执行
    pageInit();
	function pageInit(){
        //创建jqGrid组件
        jQuery("#list2").jqGrid({
	        //url : 'Global.areaQuery.features.people.town',//组件创建完成之后请求数据的url
	        //datatype : "json",//请求数据返回的类型。可选json,xml,txt
	        datatype : "local",
	        colNames : [ 'Inv No', '信息员姓名', '电话号码', '城镇', '乡村','人口','人工林','人工草地','保灌面积','其他作物','其他总产','农田面积','劳动力人口','可灌溉农田','大豆总产','大豆面积','天然总产','天然林','家禽','封闭式棚圈','小麦总产','小麦面积','山羊','干草贮草量','打草场','杂豆总产',
	                     '杂豆面积','林地','油菜总产','油菜面积','牛','猪','玉米总产','玉米面积','瓜果总产','瓜果面积','甜菜总产','甜菜面积','生产总值','秸秆总量','稻谷总产','稻谷面积','粮食作物','糜黍总产','糜黍面积','经济作物','绵羊','胡麻总产','胡麻面积','草场','荞麦总产','荞麦面积','莜麦总产','莜麦面积',
	                     '葵花总产','葵花面积','蔬菜总产','蔬菜面积','设施农业','谷子总产','谷子面积','青贮饲料量','青饲料总产','青饲料面积','马','马铃薯总产','马铃薯面积','驴','骆驼','骡','高粱总产','高粱面积'],//jqGrid的列显示名字
	        colModel : [ //jqGrid每一列的配置信息。包括名字，索引，宽度,对齐方式.....
		        {name : 'ID',index : 'ID',width : 55,align:"center",sortable : true,hidden:true},
		        {name : '信息员姓名',index:'信息员姓名',width:100,align:"right",align:"center",sortable:true},
		        {name : '电话号码',index : '电话号码 asc, invdate',width:100,align:"center",sortable : true},
		        {name : 'NAME',index : 'NAME',width:100,align:"center",sortable : true},
		        {name : 'MNAME',index:'MNAME',width:100,align:"center",align :"right"},
		        {name : '人口',index :'人口',width:80,align:"center",sortable : true},
		        {name : '人工林',index :'人工林',width:80,align:"center",sortable : true},
		        {name : '人工草地',index :'人工草地',width:80,align:"center",sortable : true},
		        {name : '保灌面积',index :'保灌面积',width:80,align:"center",sortable : true},
		        {name : '其他作物',index :'其他作物',width:50,align:"center",sortable : true},
		        {name : '其他总产',index :'其他总产',width:50,align:"center",sortable : true},
		        {name : '农田面积',index :'农田面积',width:50,align:"center",sortable : true},
		        {name : '劳动力人口',index :'劳动力人口',width:50,align:"center",sortable : true},
		        {name : '可灌溉农田',index :'可灌溉农田',width:50,align:"center",sortable : true},
		        {name : '大豆总产',index :'大豆总产',width:50,align:"center",sortable : true},
		        {name : '大豆面积',index :'大豆面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '天然总产',index :'天然总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '天然林',index :'天然林',width:50,align :"right",align:"center",sortable : true},
		        {name : '家禽',index :'家禽',width:50,align :"right",align:"center",sortable : true},
		        {name : '封闭式棚圈',index :'封闭式棚圈',width:50,align :"right",align:"center",sortable : true},
		        {name : '小麦总产',index :'小麦总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '小麦面积',index :'小麦面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '山羊',index :'山羊',width:50,align :"right",align:"center",sortable : true},
		        {name : '干草贮草量',index :'干草贮草量',width:50,align :"right",align:"center",sortable : true},
		        {name : '打草场',index :'打草场',width:50,align :"right",align:"center",sortable : true},
		        {name : '杂豆总产',index :'杂豆总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '杂豆面积',index :'杂豆面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '林地',index :'林地',width:50,align :"right",align:"center",sortable : true},
		        {name : '油菜总产',index :'油菜总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '油菜面积',index :'油菜面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '牛',index :'牛',width:50,align :"right",align:"center",sortable : true},
		        {name : '猪',index :'猪',width:50,align :"right",align:"center",sortable : true},
		        {name : '玉米总产',index :'玉米总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '玉米面积',index :'玉米面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '瓜果总产',index :'瓜果总产',width:50,align:"center",sortable : true},
		        {name : '瓜果面积',index :'瓜果面积',width:50,align:"center",sortable : true},
		        {name : '甜菜总产',index :'甜菜总产',width:50,align:"center",sortable : true},
		        {name : '甜菜面积',index :'甜菜面积',width:50,align:"center",sortable : true},
		        {name : '生产总值',index :'生产总值',width:50,align:"center",sortable : true},
		        {name : '秸秆总量',index :'秸秆总量',width:50,align:"center",sortable : true},
		        {name : '稻谷总产',index :'稻谷总产',width:50,align:"center",sortable : true},
		        {name : '稻谷面积',index :'稻谷面积',width:50,align:"center",sortable : true},
		        {name : '粮食作物',index :'粮食作物',width:50,align:"center",sortable : true},
		        {name : '糜黍总产',index :'糜黍总产',width:50,align:"center",sortable : true},
		        {name : '糜黍面积',index :'糜黍面积',width:50,align:"center",sortable : true},
		        {name : '经济作物',index :'经济作物',width:50,align:"center",sortable : true},
		        {name : '绵羊',index :'绵羊',width:50,align:"center",sortable : true},
		        {name : '胡麻总产',index :'胡麻总产',width:50,align:"center",sortable : true},
		        {name : '胡麻面积',index :'胡麻面积',width:50,align:"center",sortable : true},
		        {name : '草场',index :'草场',width:50,align:"center",sortable : true},
		        {name : '荞麦总产',index :'荞麦总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '荞麦面积',index :'荞麦面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '莜麦总产',index :'莜麦总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '莜麦面积',index :'莜麦面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '葵花总产',index :'葵花总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '葵花面积',index :'葵花面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '蔬菜总产',index :'蔬菜总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '蔬菜面积',index :'蔬菜面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '设施农业',index :'设施农业',width:50,align :"right",align:"center",sortable : true},
		        {name : '谷子总产',index :'谷子总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '谷子面积',index :'谷子面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '青贮饲料量',index :'青贮饲料量',width:50,align :"right",align:"center",sortable : true},
		        {name : '青饲料总产',index :'青饲料总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '青饲料面积',index :'青饲料面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '马',index :'马',width:50,align :"right",align:"center",sortable : true},
		        {name : '马铃薯总产',index :'马铃薯总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '马铃薯面积',index :'马铃薯面积',width:50,align :"right",align:"center",sortable : true},
		        {name : '驴',index :'驴',width:50,align :"right",align:"center",sortable : true},
		        {name : '骆驼',index :'骆驼',width:50,align :"right",align:"center",sortable : true},
		        {name : '骡',index :'骡',width:50,align :"right",align:"center",sortable : true},
		        {name : '高粱总产',index :'高粱总产',width:50,align :"right",align:"center",sortable : true},
		        {name : '高粱面积',index :'高粱面积',width:50,align :"right",align:"center",sortable : true}
		        
            ],
            rowNum : 10000,//一页显示多少条
            pager : '#pager2',//表格页脚的占位符(一般是div)的id
            multiselect: true, //是否支持多选   
            recordpos : 'left',
            rownumbers: true,	//表格增加排序列
            loadtext: "数据加载中...", 
            data:contactPeopleArray,
            //sortname : 'ID',//初始化的时候排序的字段
            //sortorder : "desc",//排序方式,可选desc,asc
            //mtype : "post",//向后台请求数据的ajax的类型。可选post,get
           // viewrecords : true,
            caption : " 责任人",//表格的标题名字
            styleUI:'Bootstrap',
            shrinkToFit:false,
            height:'370',
            width:'950',
    		autoScroll: true 
        });
        /*创建jqGrid的操作按钮容器*/
        /*可以控制界面上增删改查的按钮是否显示*/
        jQuery("#list2").jqGrid('navGrid', '#pager2', {
        	add:true,
        	del:true,
        	edit:false,
        	 addfunc:openDialog4Adding,  
             delfunc:deletemany,
             afterRefresh: openDialog4Refresh
        
        });
        
        //数据加载
    	//alert(Global.areaQuery.features.people.town);
//    	console.log(Global.areaQuery.features.people.town);
//    	//console.log(globle_contactpeoplevillage.NAME);
//    	globle_contactpeopletown=Global.areaQuery.features.people.town;
//    	var globle_contactpeoplevillage=Global.areaQuery.features.people.village;
//    	globle_contactpeopletown.push.apply(globle_contactpeopletown,globle_contactpeoplevillage);
//        //alert(globle_contactpeopletown.length);
//        console.log(globle_contactpeopletown);
//        console.log(globle_contactpeopletown.length);
//        
//        for(var i=0; i<=globle_contactpeopletown.length; i++ ){
//        	globle_contactpeople[i]=globle_contactpeopletown[i].data;
//        }
        //var arracontactpeople= new Array();
//        for(var i = 0; i<globle_contactpeopletown.length; i++){
//        	if(globle_contactpeopletown.data.MNAME=="***" || globle_contactpeopletown.data.MNAME==""){
//        		globle_contactpeopletown.data.MNAME=="城镇";
//        	}else{
//        		globle_contactpeopletown.data.MNAME=="乡村";
//        	}
//        	//alert(globle_contactpeopletown[i].data.电话号码);
//        	console.log(globle_contactpeopletown[i].data.ID);
//        }
//        for ( var i = 0; i <= globle_contactpeopletown.length; i++){
//            jQuery("#list2").jqGrid('addRowData', i + 1, globle_contactpeopletown[i].data);
//        }
    }

}
var openDialog4Adding = function() {  
    var consoleDlg = $("#consoleDlg");      
//    var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane");      
    consoleDlg.find("input").removeAttr("disabled").val("");      
//    dialogButtonPanel.find("button:not(:contains('取消'))").hide();      
//    dialogButtonPanel.find("button:contains('新增')").show();      
    consoleDlg.dialog({  
        title:"新增",  
        resizable:false,  
        width:600,  
        buttons:{  
           "取消":function(){  
               $("#consoleDlg").dialog("close");  
           },  
           "新增":addItem  
       }  
        });

    consoleDlg.dialog("open");  
};
//var openDialog4Deleting = function(rowid) {      
//	if(confirm("确认删除所选用户")){
//   	 	deleteItem(rowid);
//    } 
//}; 
    
var openDialog4Refresh = function(){
	//重新加载表格
	jQuery("#list2").jqGrid("clearGridData");
	$("#list2").setGridParam({data:contactPeopleArray}).trigger("reloadGrid");
};
var addItem = function(){  
    var consoleDlg = $("#consoleDlg");  
 	var informationname = $.trim(consoleDlg.find("#informationname").val());  
    var telphoneinfo = $.trim(consoleDlg.find("#telphoneinfo").val());  
  /*  var contactAddress = $.trim(consoleDlg.find("#contactAddress").val());  
    var contactPeople = $.trim(consoleDlg.find("#contactPeople").val());  
    var contactPhone = $.trim(consoleDlg.find("#contactPhone").val());  
    var installDate = $.trim(consoleDlg.find("#installDate").val());  
    var displayType = $.trim(consoleDlg.find("#displayType").val());  
    var displayAttribute = $.trim(consoleDlg.find("#displayAttribute").val());  
    var displayStatus = $.trim(consoleDlg.find("#displayStatus").val());
    var displayId = $.trim(consoleDlg.find("#displayId").val());
    var organization = $.trim(consoleDlg.find("#organization").val());
    var industry = $.trim(consoleDlg.find("#industry").val());
    var gprsUrl = $.trim(consoleDlg.find("#gprsUrl").val());
    var province = $.trim(consoleDlg.find("#province").val());
    var functions = $.trim(consoleDlg.find("#functions").val());
    var lon = $.trim(consoleDlg.find("#lon").val());
    var lat = $.trim(consoleDlg.find("#lat").val());*/
    if(informationname == ""){
    	alert("责任人姓名不为空！");
    	return;
    }else if((telphoneinfo != "" && !(/^1[34578]\d{9}$/.test(telphoneinfo))) && (telphoneinfo != "" &&!(/^0\d{2,3}-\d{7,8}$/.test(telphoneinfo)) )){
        alert("联系电话格式如:0371-68787027 或 13222444567");    	
        return;
    }
//    for(var i=0; i<= globle_contactpeopletown.length; i++){
//    	globle_contactpeople[i]=globle_contactpeopletown[i].data;
//    	alert(globle_contactpeople[i].ID);
//    }
//    $(globle_contactpeople).each(function(index,value){
//    	alert(value.信息员姓名);
//    })
//    for(var i=0; i<=globle_contactpeople.length;i++){
//    	alert(globle_contactpeople[i].信息员姓名);
//    }
    var contactid=contactPeopleArray.length+1;
    var person=new Object();
    person.ID=contactid;
    person.信息员姓名=informationname;
    person.电话号码=telphoneinfo;
    
    contactPeopleArray.unshift(person);
    //contactPeopleArray.push(person);
    //console.log(contactPeopleArray);
    //重新加载表格
    jQuery("#list2").jqGrid("clearGridData");
    //alert(displayArrayData);
    $("#list2").setGridParam({data:contactPeopleArray}).trigger("reloadGrid");   
    $("#consoleDlg").dialog("close"); 
}
/*
var deleteItem = function(rowid){  
    var ID = $("#gridTable").jqGrid("getRowData", rowid)["ID"]; 
    for(var i=0;i<=globle_contactpeopletown.length;i++){
    	if(globle_contactpeopletown[i].data.ID=ID){
    		globle_contactpeopletown.splice(1,1);
    	}
    }
    for(var i = 0; i <= globle_contactpeopletown.length; i++){
        jQuery("#list2").jqGrid('addRowData', i + 1, globle_contactpeopletown[i].data);
    }
    jQuery("#gridTable").jqGrid("clearGridData");
    $("#gridTable").setGridParam({}).trigger("reloadGrid");    
           
} */
var deletemany=function(){
	var ids=$("#list2").jqGrid("getGridParam","selarrrow");
	if(confirm("确认删除所选"+ids.length+"条责任人信息？")){
		$(ids).each(function(index,id){
	    	var peoid = $("#list2").jqGrid("getRowData",id)["ID"];
	    	$(contactPeopleArray).each(function(index,value){
	    		if(value.ID == peoid){
	    			contactPeopleArray.splice(index--,1);
	    		}
	    	});
		});
	    jQuery("#list2").jqGrid("clearGridData");
	    $("#list2").setGridParam({data:contactPeopleArray}).trigger("reloadGrid");    
	} 
}
